import { ref } from 'vue'
import JSZip from "jszip";
import { getSelectedStudentFileApi, getSelectedTeacherFileApi } from '@/api/file'
const dataURLtoBlob = (dataurl: string) => {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) { u8arr[n] = bstr.charCodeAt(n); }
    return new Blob([u8arr], { type: mime });
}
export function downloadStuFile(fileName: string, task_id: string, user_id: string) {
    const params = ref({
        file_name: fileName,
        task_id: task_id,
        user_id: user_id,
    })
    getSelectedStudentFileApi(params.value).then((res: any) => {
        const blob = dataURLtoBlob(res.data)
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    })
}
export function downloadTeaFile(fileName: string, task_id: string) {
    const params = ref({
        file_name: fileName,
        task_id: task_id,
    })
    getSelectedTeacherFileApi(params.value).then((res: any) => {
        const blob = dataURLtoBlob(res.data)
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    })
}
// export default downloadStuFile